﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ConstantCMapEditor3
{
    static public class MouseManager
    {
        static Random random = new Random();

        public static void Update() 
        {
           // Mouse.GetState
            MouseState currentState= Mouse.GetState();

            mouseEvent = new MouseEvent();
            mouseEvent.ClickDownEdgeL = false;
            mouseEvent.ClickDownEdgeR = false;
            mouseEvent.ClickDownEdgeM = false;

            mouseEvent.ClickUpEdgeL = false;
            mouseEvent.ClickUpEdgeR = false;
            mouseEvent.ClickUpEdgeM = false;
            mouseEvent.WheelUp = false;
            mouseEvent.WheelDown = false;

            if (preState != null)
            {
                if (preState.ScrollWheelValue > currentState.ScrollWheelValue) mouseEvent.WheelUp = true;
                if (preState.ScrollWheelValue < currentState.ScrollWheelValue) mouseEvent.WheelDown = true;

                if (preState.LeftButton == ButtonState.Released && currentState.LeftButton == ButtonState.Pressed)
                {
                    mouseEvent.ClickDownEdgeL = true;
                    preID = random.Next();
                }


                if (preState.RightButton == ButtonState.Released && currentState.RightButton == ButtonState.Pressed)
                {
                    mouseEvent.ClickDownEdgeR = true;
                    preID = random.Next();
                }

                if (preState.MiddleButton == ButtonState.Released && currentState.MiddleButton == ButtonState.Pressed)
                {
                    mouseEvent.ClickDownEdgeM = true;
                    preID = random.Next();
                }


                if (preState.LeftButton == ButtonState.Pressed && currentState.LeftButton == ButtonState.Released)
                {
                    mouseEvent.ClickUpEdgeL = true;
                   
                }

                if (preState.RightButton == ButtonState.Pressed && currentState.RightButton == ButtonState.Released)
                {
                    mouseEvent.ClickUpEdgeR = true;
                   
                }

                if (preState.MiddleButton== ButtonState.Pressed && currentState.MiddleButton == ButtonState.Released)
                {
                    mouseEvent.ClickUpEdgeM = true;
               
                }
            }
            else 
            {
                mouseEvent = new MouseEvent();
            }

            mouseEvent.Position = new Vector2(currentState.X, currentState.Y);
            mouseEvent.PrePosition = new Vector2(preState.X, preState.Y);
            mouseEvent.ClickID = preID;
            mouseEvent.PressedL = currentState.LeftButton == ButtonState.Pressed;
            mouseEvent.PressedR = currentState.RightButton == ButtonState.Pressed;
            mouseEvent.PressedM = currentState.MiddleButton == ButtonState.Pressed;

            preState = currentState;
        }

        static public MouseEvent mouseEvent;
        static public MouseState preState;
        static public int preID;
    }

    public struct MouseEvent
    {
        public int ClickID;
        public Vector2 Position;
        public Vector2 PrePosition;

        public bool ClickDownEdgeL;
        public bool ClickUpEdgeL;
        public bool PressedL;

        public bool ClickDownEdgeR;
        public bool ClickUpEdgeR;
        public bool PressedR;


        public bool ClickDownEdgeM;
        public bool ClickUpEdgeM;
        public bool PressedM;

        public bool WheelUp;
        public bool WheelDown;
    }

    static public class KeyBoardManager 
    {
        static private KeyboardState preKeyboard;

        static public bool IsKeyEdgeUp(Keys key)
        {
            return preKeyboard.IsKeyDown(key)&&Keyboard.GetState().IsKeyUp(key);
        }

        static public bool IsKeyEdgeDown(Keys key)
        {
            return preKeyboard.IsKeyUp(key) && Keyboard.GetState().IsKeyDown(key);
        }

        static public void UpdateKeyboard()
        {
            preKeyboard = Keyboard.GetState();
        }

    }
}
